const works = []
import tokens from '../token'
import groups from './group'
const steptimes = new Map([
    ["1h", () => 60 * 60 * 1000],
    ["1d", () => steptimes.get('1h')() * 24],
    ["7d", () => steptimes.get('1d')() * 7],
    ["1m", () => steptimes.get('1d')() * 30],
    ["1y", () => steptimes.get('1d')() * 365]
])
function workchild({ starttime, steptime, worklen }) {
    let childrens = [];
    let lasttime = starttime;
    for (let i = 0; i < worklen; i++) {
        let endtime = new Date(Number(new Date(lasttime)) + steptimes.get(steptime)());
        childrens.push({ index: i + 1, starttime: lasttime, endtime: endtime, })
        lasttime = endtime;
    }
    return {childrens,endtime:lasttime}
}
function findGroups(){

}
export default [{//添加作业
    path: "/api/work/create",
    methods: "post",
    handle({ body }) {
        let len = works.length;
        try {
            const {childrens,endtime}=workchild(body)
            works.push({ id: len + 1, ...body, childrens: childrens,endtime,teacherid:tokens[body.token].id})
        } catch (error) {
            console.log(error);
            return {
                code: 500,
                msg: "服务器错误",
                data: {}
            }
        }
        return {
            code: 0,
            msg: "发布成功",
            data: {}
        }
    }
}, { //查询最新作业，按时间查询
    path: "/api/work/selectItemByWorkid",
    methods: "post",
    handle({ body }) {
        let workslen=works.length
        const work = works.find(e=>e.id===body.workid)
        return {
            code: 0,
            msg: "查询成功",
            data: work
        }
    }
}, { //查询老师发布全部作业，按老师id进行查询，按时间进行降序
    path: "/api/work/selectworkItemByTeacher",
    methods: "post",
    handle({ body }) {
        const work = works.filter(e=>e.teacherid==tokens[body.token].id)
        return {
            code: 0,
            msg: "查询成功",
            data: work
        }
    }
}]